package com.ize.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ize.entity.dto.AvailableSlotsQueryDTO;
import com.ize.entity.dto.BatchOperationDTO;
import com.ize.entity.dto.ReservationQueryDTO;
import com.ize.entity.dto.ReservationStatisticsDTO;
import com.ize.entity.pojo.Reservation;
import com.ize.result.Result;
import com.ize.service.YuYueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/appointment")
@Slf4j
@Api(tags = "预约相关接口")
@RequiredArgsConstructor
public class YuYueController {
    private final YuYueService yuYueService;

    @GetMapping("/list")
    @ApiOperation("获取预约列表")
    public Result<Page<Reservation>> list(ReservationQueryDTO query) {
        log.info("查询预约列表: {}", query);
        Page<Reservation> page = yuYueService.getReservationPage(query);
        return Result.success(page);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取预约详情")
    public Result<Reservation> getById(@PathVariable Long id) {
        log.info("查询预约详情, id: {}", id);
        Reservation reservation = yuYueService.getReservationById(id);
        if (reservation == null) {
            return Result.error("预约不存在");
        }
        return Result.success(reservation);
    }

    @PostMapping("/add")
    @ApiOperation("创建预约")
    public Result<Boolean> add(@RequestBody Reservation reservation) {
        log.info("创建预约: {}", reservation);
        boolean success = yuYueService.createReservation(reservation);
        return success ? Result.success(true) : Result.error("创建预约失败");
    }

    @PutMapping("/update/{id}")
    @ApiOperation("更新预约信息")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody Reservation reservation) {
        log.info("更新预约信息, id: {}, data: {}", id, reservation);
        boolean success = yuYueService.updateReservation(id, reservation);
        return success ? Result.success(true) : Result.error("更新预约失败");
    }

    @PutMapping("/status/{id}")
    @ApiOperation("更新预约状态")
    public Result<Boolean> updateStatus(@PathVariable Long id, @RequestBody Map<String, String> statusData) {
        String status = statusData.get("status");
        String remark = statusData.get("remark");
        log.info("更新预约状态, id: {}, status: {}, remark: {}", id, status, remark);

        if (status == null || status.isEmpty()) {
            return Result.error("状态不能为空");
        }

        boolean success = yuYueService.updateReservationStatus(id, status, remark);
        return success ? Result.success(true) : Result.error("更新状态失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除预约")
    public Result<Boolean> delete(@PathVariable Long id) {
        log.info("删除预约, id: {}", id);
        boolean success = yuYueService.deleteReservation(id);
        return success ? Result.success(true) : Result.error("删除预约失败");
    }

    @PostMapping("/batch")
    @ApiOperation("批量操作预约")
    public Result<Boolean> batchOperate(@RequestBody BatchOperationDTO batchOperation) {
        log.info("批量操作预约: {}", batchOperation);
        boolean success = yuYueService.batchOperateReservation(batchOperation);
        return success ? Result.success(true) : Result.error("批量操作失败");
    }

    @GetMapping("/available-slots")
    @ApiOperation("获取可预约时间段")
    public Result<List<String>> getAvailableSlots(AvailableSlotsQueryDTO query) {
        log.info("查询可用预约时间段: {}", query);
        List<String> slots = yuYueService.getAvailableTimeSlots(query);
        return Result.success(slots);
    }

    @GetMapping("/statistics")
    @ApiOperation("获取预约统计数据")
    public Result<ReservationStatisticsDTO> getStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long doctorId) {
        log.info("查询预约统计数据, startDate: {}, endDate: {}, doctorId: {}", startDate, endDate, doctorId);
        ReservationStatisticsDTO statistics = yuYueService.getStatistics(startDate, endDate, doctorId);
        return Result.success(statistics);
    }
}
